#include "timer_it.h"

TIM_HandleTypeDef htim16;

/**
 * @brief TIM16定时器中断初始化，1ms中断1次
 * @note 系统频率24MHz
 */
void timer16_it_init(void)
{
    htim16.Instance = TIM16;
    htim16.Init.Prescaler = 12 - 1;
    htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim16.Init.Period = 2000 - 1;
    htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim16.Init.RepetitionCounter = 0;
    htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_TIM_Base_Init(&htim16);

    __HAL_TIM_CLEAR_IT(&htim16,TIM_IT_UPDATE);//清除定时器初始化过程中的更新中断标志位，避免定时器一启动就触发中断
    HAL_TIM_Base_Start_IT(&htim16);
}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
{
    if (htim->Instance == TIM16)
    {
        __HAL_RCC_TIM16_CLK_ENABLE();
        HAL_NVIC_SetPriority(TIM16_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(TIM16_IRQn);
    }
}

// 定时器中断回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if (htim->Instance == TIM16)
    {
        //   printf("TIM16\n");
    }
}